/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.asiainfo.utils.sql.sqlbuilder.select;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

public class GroupBy implements TerminalExpression {
	private final Context context;
	private final List<String> columns;

	public GroupBy(Context context) {
		this.context = context;
		context.append("GROUP BY");
		this.columns = new LinkedList();
	}

	public GroupBy column(String column) {
		this.columns.add(column);
		return this;
	}

	public Having having() {
		end();
		return new Having(this.context);
	}

	public <E> List<E> list(RowMapper<E> rowMapper) throws SQLException {
		end();
		return this.context.list(rowMapper);
	}

	public <E> E single(RowMapper<E> rowMapper) throws SQLException {
		end();
		return this.context.single(rowMapper);
	}

	private void end() {
		this.context.append(StringUtils.join(this.columns, ", "));
	}
}